#include <stdio.h>

char board[22][22];
int r, s;
char prev[30];
int numLett;

void DFS(int i, int j, int num) {
	for (int k=0; k<num; k++)
		if (board[i][j]==prev[k])
			return;

	prev[num]=board[i][j];
	num++;

	if (numLett < num) numLett = num;

	if (i+1<r) 
		DFS(i+1, j, num);
	if (i-1>=0)
		DFS(i-1, j, num);
	if (j+1<s)
		DFS(i, j+1, num);
	if (j-1>=0)
		DFS(i, j-1, num);
}

int main() {
	scanf("%d %d", &r, &s);

	for (int i=0; i<r; i++) {
		for (int j=0; j<s; j++)
			scanf(" %c", &board[i][j]);
	}

	DFS(0, 0, 0);

	printf("%d\n", numLett);

	return 0;
}
